package net.trustie.model;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

@ExtractBy("//div[@class='realbody']/div[@class='main']")
public class gna_Model implements ValidateExtractor, AfterExtractor {
	
	private String project_url = "";
	
	private String project_name = "";
	
	@ExtractBy("//div[@class='indexcenter']/allText()")
	private String project_description = "";
	
	private String crawled_time = null;
	
	private String extractTime = null;
	
	private String project_created_time = null;
	
	private String project_updated_time = null;
	
	private int history = 0;
	
	@Override
	public void afterProcess(Page page) {
		// TODO Auto-generated method stub
		project_url = page.getPageUrl();
		int index = project_url.lastIndexOf("/");
		project_name = project_url.substring(index + 1).trim();
		
		int index_time = project_description.lastIndexOf("Registration Date:");
		if(index_time > 0){
			String tmp = project_description;
			project_description = project_description.substring(0, index_time).trim();
			
			String time = StringHandler.matchRightString(tmp, "[a-zA-Z]{3}\\s([a-zA-Z]{3}|[a-zA-Z]{4})\\s(\\d{1}|\\d{2})\\s\\d{2}:\\d{2}:\\d{2}\\s\\d{4}");
			@SuppressWarnings("deprecation")
			Date registerTime = new Date(time);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			project_created_time = sdf.format(registerTime);
			
		}else{
			project_description = project_description.trim();
		}
		
		extractTime = DateHandler.getExtractTime();
		
	}

	@Override
	public void validate(Page page) {
		// TODO Auto-generated method stub
		if (StringHandler.isAtLeastOneBlank(this.project_url, this.project_name)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!DateHandler.canFormatToDate(this.project_created_time, this.extractTime))
			page.setResultSkip(this, true);	
	}

	public String getProject_url() {
		return project_url;
	}

	public void setProject_url(String project_url) {
		this.project_url = project_url;
	}

	public String getProject_name() {
		return project_name;
	}

	public void setProject_name(String project_name) {
		this.project_name = project_name;
	}

	public String getProject_description() {
		return project_description;
	}

	public void setProject_description(String project_description) {
		this.project_description = project_description;
	}

	public String getCrawled_time() {
		return crawled_time;
	}

	public void setCrawled_time(String crawled_time) {
		this.crawled_time = crawled_time;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getProject_created_time() {
		return project_created_time;
	}

	public void setProject_created_time(String project_created_time) {
		this.project_created_time = project_created_time;
	}

	public String getProject_updated_time() {
		return project_updated_time;
	}

	public void setProject_updated_time(String project_updated_time) {
		this.project_updated_time = project_updated_time;
	}

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}
	
}
